<template>
  <div>
    <!-- 顶部 -->
    <el-card>
      <Category @getCategoryId="getCategoryId"></Category>
    </el-card>
    <!-- 底部 -->
    <el-card style="margin: 20px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttr"
          :disabled="!category3Id"
          >添加属性</el-button
        >
        <!-- 展示品台属性table列表地方 -->
        <el-table style="width: 100%; margin: 15px 0" border :data="attrList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="200">
          </el-table-column>
          <el-table-column label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <!-- row 平台属性展示平台属性 -->
              <el-tag
                type="success"
                v-for="attrValue in row.attrValueList"
                :key="attrValue.id"
                style="margin: 0 5px"
              >
                {{ attrValue.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="scene == 1">
        <!-- 行内form -->
        <el-form ref="form" :inline="true" :model="attrForm">
          <el-form-item label="属性名">
            <el-input v-model="attrForm.attrName"></el-input>
          </el-form-item>
        </el-form>
        <!-- 两个按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttrValue"
          :disabled="!(attrForm.attrName.length > 0)"
          >添加属性值</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
        <!-- table -->
        <el-table
          style="width: 100%; margin: 15px 0"
          border
          :data="attrForm.attrValueList"
        >
          <el-table-column type="index" align="center" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 实现编辑模式与查看模式 -->

              <el-input
                v-model="row.valueName"
                :ref="$index"
                size="mini"
                v-if="row.show"
                @blur="toLook(row)"
                @keyup.enter.native="toLook(row)"
              ></el-input>
              <div @click="toEdit(row, $index)" v-else>{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row, $index}">
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="attrForm.attrValueList.splice($index,1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          :disabled="!(attrForm.attrValueList.length > 0)"
          @click="addOrUpdateSave"
          >保存</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 平台属性存储属性
      attrList: [],
      // 定义一个数据,用来控制显示内容(添加|修改)+展示
      scene: 0,
      attrForm: {
        // 属性的名字
        attrName: "",
        // 属性值的地方
        attrValueList: [
          // {
          //   attrId: 0, //属性的Id
          //   valueName: "",
          // },
        ],
        categoryId: "", //代表是三级分类的Id
        categoryLevel: 3,
      },
    };
  },
  methods: {
    getCategoryId({ id, flag }) {
      switch (flag) {
        case 1:
          this.category1Id = id;
          // 清空二,三级分类的id
          this.category2Id = "";
          this.category3Id = "";
          break;
        case 2:
          this.category2Id = id;
          this.category3Id = "";
          break;
        case 3:
          this.category3Id = id;
          // 当三级分类的内容确定以后发请求,获取品牌属性的数据
          this.getAttrList();
          break;
      }
    },
    // 获取品台属性的方法
    async getAttrList() {
      let { category1Id, category2Id, category3Id } = this;
      let result = await this.$ajax.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (result.code == 200) {
        this.attrList = result.data;
      }
    },
    // 添加属性按钮的回调函数
    addAttr() {
      // 切换场景
      this.scene = 1;
      // 每次切换场景的时候将数据清空
      // 收集到三级分类的id
      this.attrForm = {
        // 属性的名字
        attrName: "",
        // 属性值的地方
        attrValueList: [], //
        categoryId: this.category3Id, //代表的是三级分类的id
        categoryLevel: 3, //永远是3,给后台的标记
      };
    },
    // 修改属性的回调
    updateAttr(row) {
      this.scene = 1;
      // 将修改的属性赋值给attrForm
      this.attrForm=cloneDeep(row);


      //第二种解决方案:替换
      this.attrForm.attrValueList= this.attrForm.attrValueList.map(item=>{
        return {
          ...item,
          show:false
        }
      }) 
    },
    // 添加属性指的回调
    addAttrValue() {
      // 向数组AttrValueList添加元素
      this.attrForm.attrValueList.push({
        attrId: undefined, //当前属性的id
        valueName: "", //当前值得名称
        show: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrForm.attrValueList.length - 1].focus();
      });
    },
    // 切换为查看模式
    toLook(row) {
      // 当失去焦点的时候:属性值不能为空
      if (row.valueName.trim() == "") {
        this.$message({ type: "warning", message: "请不要输入空内容" });
        return;
      }
      // 查看新增的属性值是否出现过
      let repeat = this.attrForm.attrValueList.find((item) => {
        if (item != row) {
          return item.valueName == row.valueName;
        }
      });
      if (repeat) return;
      // 变为查看模式--false
      row.show = false;
    },
    // 切换为编辑模式
    toEdit(row, index) {
      // 编辑模式
      row.show = true;
      // 通过ref获取input组件,让它聚焦
      // nextTick:在修改数据之后立即使用这个方法,获取更新后的DOM
      // 使用延时器,延迟获取el-input组件
      this.$nextTick(() => {
        // 获取input组件
        this.$refs[index].focus();
      });
    },
    // 发请求:新增修改
    async addOrUpdateSave() {
      // 整理参数
      // 属性的属性值如果为空不应该带给服务器
      // 属性的属性值如果可以带给服务器,show字段需要移除
      this.attrForm.attrValueList = this.attrForm.attrValueList.filter(
        (item) => {
          // 代表带给服务器的参数:valueName属性不为空的
          if (item.valueName != "") {
            delete item.show;
            return true;
          }
        }
      );
      // 发请求:新增|修改
      try {
        // 新增|修改成功
        await this.$ajax.attr.reqAddOrUpdateAttr(this.attrForm);
        // 切换场景
        this.scene = 0;
        // 再次获取平台属性的最新的数据
        this.getAttrList();
      } catch (error) {
        //
        this.$message({ type: "error", message: "新增失败" });
      }
    },
  },
};
</script>

<style>
</style>